/**
 * 消息模板 store
 */
import { action, observable } from 'mobx';
import { MessageTemplateService } from '../services';

class MessageTemplateStore {
  @observable messageTemplates = [];
  @observable currentTemplate = null;
  @observable isModalVisible = false;
  @observable isEditing = false;
  @observable editingTemplate = {
    id: null,
    title: '',
    content: '',
    msgType: 'text',
  };

  // 获取消息模板列表
  @action getMessageTemplates = () => {
    MessageTemplateService.getMessageTemplates().then((result) => {
      if (result.data.code !== 0) return;
      this.messageTemplates = result.data.data;
    });
  }

  // 选择消息模板
  @action selectTemplate = (template) => {
    this.currentTemplate = template;
  }

  // 显示/隐藏模态框
  @action toggleModal = (visible) => {
    console.log('toggleModal called with:', visible);
    this.isModalVisible = visible;
    if (!visible) {
      this.resetEditingTemplate();
    }
  }

  // 设置编辑状态
  @action setEditing = (isEditing, template = null) => {
    console.log('setEditing called with:', isEditing, template);
    this.isEditing = isEditing;
    if (isEditing && template) {
      this.editingTemplate = { ...template };
    } else {
      this.resetEditingTemplate();
    }
  }

  // 更新编辑中的模板
  @action updateEditingTemplate = (field, value) => {
    this.editingTemplate[field] = value;
  }

  // 重置编辑中的模板
  @action resetEditingTemplate = () => {
    this.editingTemplate = {
      id: null,
      title: '',
      content: '',
      msgType: 'text',
    };
    this.isEditing = false;
  }

  // 创建消息模板
  @action createMessageTemplate = () => {
    const { title, content, msgType } = this.editingTemplate;
    
    return new Promise((resolve, reject) => {
      MessageTemplateService.createMessageTemplate({ title, content, msgType })
        .then((result) => {
          if (result.data.code === 0) {
            this.getMessageTemplates();
            this.toggleModal(false);
            resolve(result);
          } else {
            reject(result.data.msg);
          }
        })
        .catch((err) => {
          reject(err);
        });
    });
  }

  // 更新消息模板
  @action updateMessageTemplate = () => {
    const { id, title, content, msgType } = this.editingTemplate;
    
    return new Promise((resolve, reject) => {
      MessageTemplateService.updateMessageTemplate({ id, title, content, msgType })
        .then((result) => {
          if (result.data.code === 0) {
            this.getMessageTemplates();
            this.toggleModal(false);
            resolve(result);
          } else {
            reject(result.data.msg);
          }
        })
        .catch((err) => {
          reject(err);
        });
    });
  }

  // 删除消息模板
  @action deleteMessageTemplate = (id) => {
    return new Promise((resolve, reject) => {
      MessageTemplateService.deleteMessageTemplate({ id })
        .then((result) => {
          if (result.data.code === 0) {
            this.getMessageTemplates();
            resolve(result);
          } else {
            reject(result.data.msg);
          }
        })
        .catch((err) => {
          reject(err);
        });
    });
  }
}

export default new MessageTemplateStore(); 